#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python_learning 
@File ：异步实际应用案例——获取网页.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/7/18 上午9:46 
"""

import asyncio

import aiohttp  # 异步HTTP库


async def fetch_url(session, url):
	print(f"开始请求: {url}")
	async with session.get(url) as response:
		data = await response.text()  # await 等待响应内容
		print(f"完成请求: {url}")
		return data


async def main():
	urls = [
		'https://httpbin.org/delay/1',
		'https://httpbin.org/delay/2',
		'https://httpbin.org/delay/3'
	]

	async with aiohttp.ClientSession() as session:
		# 并发执行多个HTTP请求
		tasks = [fetch_url(session, url) for url in urls]
		results = await asyncio.gather(*tasks)

	print("所有请求完成")

if __name__ == '__main__':
	asyncio.run(main())
